import json
import re
import time
from DrissionPage import ChromiumPage
from DrissionPage._functions.keys import Keys
import os
import shutil
from util.logger import get_looger

logger = get_looger("起点.log", __file__)


def clean_filename(filename):
    """清理文件名中的非法字符"""
    # 先移除换行符和制表符
    filename = re.sub(r'[\n\r\t]', ' ', filename)
    # 移除Windows文件名中的非法字符
    filename = re.sub(r'[<>:"/\\|?*]', '', filename)
    # 移除多余的空格，限制文件名长度
    filename = re.sub(r'\s+', ' ', filename).strip()
    # 限制文件名长度，避免路径过长
    if len(filename) > 50:
        filename = filename[:50] + "..."
    return filename


logger.info("爬虫启动......")

try:
    root = 'static/起点/'
    if os.path.exists(root):
        shutil.rmtree(root)
    os.makedirs(root)

    # path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
    # ChromiumOptions().set_browser_path(path).save()

    dp = ChromiumPage()
    dp.get('https://www.qidian.com')
    dp.scroll.to_bottom()
    time.sleep(2)
    lis = dp.eles('css:.rank-list-row')

    for index, li in enumerate(lis, 1):
        content = li.text
        print(content)

        # 生成安全的文件名：序号 + 清理后的前几个字符
        if content:
            # 提取第一行或前几个字符作为文件名
            first_line = content.split('\n')[0] if '\n' in content else content
            clean_name = clean_filename(first_line)
            filename = f"{index:02d}_{clean_name}.txt"
        else:
            filename = f"{index:02d}_未知榜单.txt"

        filepath = os.path.join(root, filename)

        with open(filepath, "w", encoding="utf-8") as f:
            f.write(content)

        logger.info(f"已保存: {filename}")

    logger.info("爬虫完成！")

except Exception as e:
    logger.error(e)